package com.example.demo3.controller.purplan;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.controller.BaseController;
import com.example.demo3.entity.ResourceDevice;
import com.example.demo3.entity.purplan.PurPlan;
import com.example.demo3.service.ResourceDeviceService;
import com.example.demo3.service.purplan.PurPlanService;
import com.example.demo3.utils.excelprocess.POIHelper;
import com.example.demo3.utils.strprocess.ObjectParse;
import com.example.demo3.utils.strprocess.RespStruct;
import com.example.demo3.utils.strprocess.StrUtil;
import com.example.demo3.utils.strprocess.Time_convert;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Struct;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 采购计划表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2020-03-23
 */
@Controller
@RequestMapping(value = PurPlanController.url_prefix)
public class PurPlanController extends BaseController {
	
	public static final String url_prefix="/purPlan";
	
	@Resource
	private PurPlanService purPlanService;
	
	
	/**
	 * 
	 * @param 
	 * @return 
	 * @author cjw 2020-03-23
	 */
	@RequestMapping(value = "/list")
	public String list(){
		return url_prefix+"/list";
	}
	
	/**
	 *
	 * 传入参数: status(0 未进行; 1 已结束; 2 所有(默认值);)
	 * @param 
	 * @return 
	 * @author cjw 2020-03-24
	 */
	@RequestMapping(value = "/getPurPlanByPage")
	@ResponseBody
	public DataPageMessage getPurPlanByPage(@RequestParam Map<String,Object> map){
		
		StrUtil.prettyPrint("getPurPlanByPage().map",map);
		
		int pageNum=0;
		int pageSize=0;
		int sEcho=0;
		String planNo=null;
		String createTimeStartStr=null;
		String createTimeEndStr=null;
		int status=0;
		try{
			pageNum= ObjectParse.parse_toInt(map.get("pageindex"));
			pageSize=ObjectParse.parse_toInt(map.get("pagesize"));
			sEcho=ObjectParse.parse_toInt(map.get("sEcho"));
			
			planNo=ObjectParse.parse_toStr(map.get("planNo"));
			createTimeStartStr=ObjectParse.parse_toStr(map.get("createTimeStart"));
			createTimeEndStr=ObjectParse.parse_toStr(map.get("createTimeEnd"));
			status=ObjectParse.parse_toInt(map.get("status"),2);
			
		}catch (Exception e){
			return DataPageMessage.error("传入的参数有误!");
		}
		
		Page<PurPlan> page=purPlanService.getPurPlanByPage(pageNum,pageSize,planNo,createTimeStartStr,createTimeEndStr,status);
		
		//StrUtil.prettyPrint("page",page);
		
		return DataPageMessage.ok(
				page.getRecords(),
				(int)page.getTotal(),
				(int)page.getTotal(),
				sEcho,
				"请求成功!");
		
	}
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-24
	 */
	@RequestMapping(value = "/getPurPlan")
	@ResponseBody
	public DataPageMessage getPurPlan(@RequestParam Map<String,Object> map){
		
		StrUtil.prettyPrint("getPurPlan().map",map);
		
		int pageNum=0;
		int pageSize=0;
		int sEcho=0;
		try{
			pageNum= ObjectParse.parse_toInt(map.get("pageindex"));
			pageSize=ObjectParse.parse_toInt(map.get("pagesize"));
			sEcho=ObjectParse.parse_toInt(map.get("sEcho"));
		}catch (Exception e){
			return DataPageMessage.error("传入的参数有误!");
		}
		
		List<PurPlan> list_purPlan=purPlanService.getPurPlanList();
		
		return DataPageMessage.ok(
				list_purPlan,
				list_purPlan.size(),
				list_purPlan.size(),
				sEcho,
				"请求成功!"
		);
	}
	
	/**
	 * 
	 * @param
	 * @return 
	 * @author cjw 2020-03-25
	 */
	@RequestMapping("/edit")
	public String edit(Model model, String purPlanId){
		//验证传来的参数
		try{
			purPlanId=ObjectParse.parse_toStr_elseException(purPlanId);
		}catch (Exception e){
			throw new RuntimeException("传入的参数有误!");
		}
		
		PurPlan purPlan=purPlanService.getPurPlanById(purPlanId);
		model.addAttribute("purPlan",purPlan);
		return url_prefix+"/edit";
	}
	
	/**
	 * 
	 * @param 
	 * @return 
	 * @author cjw 2020-03-25
	 */
	@RequestMapping("/updatePurPlan")
	@ResponseBody
	public ResponseMessage updatePurPlan(@RequestParam Map<String,Object> map){
		
		StrUtil.prettyPrint("updatePurPlan().map",map);
		
		String id=null;
		String planNo=null;
		String title=null;
		try{
			id=ObjectParse.parse_toStr_elseException(map.get("id"));
			planNo=ObjectParse.parse_toStr_elseException(map.get("planNo"));
			title=ObjectParse.parse_toStr_elseException(map.get("title"));
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!");
		}
		
		PurPlan purPlan=purPlanService.getPurPlanById(id);
		if(purPlan==null){
			return ResponseMessage.error("未找到对应的purPlan信息!");
		}
		
		purPlan.setPlanNo(planNo);
		purPlan.setTitle(title);
		purPlan.setUpdateTime(new Date());
		purPlan.setUpdateUser("cjw");
		purPlanService.updateById(purPlan);
		
		return ResponseMessage.ok("请求成功!");
	}
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-25
	 */
	@RequestMapping("/delPurPlanById")
	@ResponseBody
	public ResponseMessage delPurPlanById(String purPlanId){
		
		System.out.println("执行了delPurPlanById()方法..");
		
		//解析传来的参数
		try{
			purPlanId=ObjectParse.parse_toStr_elseException(purPlanId);
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		
		purPlanService.removeById(purPlanId);
		return ResponseMessage.ok();
	}
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-25
	 */
	@RequestMapping(value = "/add")
	public String add(){
		return url_prefix+"/add";
	}
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-25
	 */
	@RequestMapping(value = "/addPurPlan")
	@ResponseBody
	public ResponseMessage addPurPlan(@RequestParam Map<String,Object> map){
		
		StrUtil.prettyPrint("addPurPlan().map",map);
		
		//解析传来的参数
		String planNo=null;
		String title=null;
		try{
			planNo=ObjectParse.parse_toStr_elseException(map.get("planNo"));
			title=ObjectParse.parse_toStr_elseException(map.get("title"));
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!");
		}
		
		PurPlan purPlan=new PurPlan();
		purPlan.setPlanNo(planNo);
		purPlan.setTitle(title);
		purPlan.setCreateTime(new Date());
		purPlan.setCreateUser("cjw");
		purPlanService.save(purPlan);
		return ResponseMessage.ok();
	}
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2020-03-26
	 */
	@RequestMapping("/delPurPlanBatch")
	@ResponseBody
	public ResponseMessage delPurPlanBatch(String purPlanIds){
		//解析传来的参数
		try{
			purPlanIds=ObjectParse.parse_toStr_elseException(purPlanIds);
		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!"+e.toString());
		}
		
		String[] array_purPlanId=purPlanIds.split(",|\\|");
		purPlanService.removeByIds(Arrays.asList(array_purPlanId));
		return ResponseMessage.ok();
	}
	
	/**
	 * 导出采购计划》导出Execl,供list页面使用。
	 *
	 * @param
	 * @return
	 * @author cjw 2019-08-07
	 */
	@RequestMapping(value = "/exportExecl")
	public void exportExecl(HttpServletResponse response, @RequestParam Map<String, Object> map) {
		
		StrUtil.prettyPrint("exportExecl().map", map);
		
		//解析传来的参数
		String planNo = null;
		String createTimeStartStr = null;
		String createTimeEndStr = null;
		int status = 0;
		try {
			planNo = ObjectParse.parse_toStr(map.get("planNo"));
			createTimeStartStr = ObjectParse.parse_toStr(map.get("createTimeStart"));
			createTimeEndStr = ObjectParse.parse_toStr(map.get("createTimeEnd"));
			status = ObjectParse.parse_toInt(map.get("status"), 2);
			
		} catch (Exception e) {
			throw new RuntimeException("传入的参数有误!" + e.toString());
		}
		
		List<PurPlan> list_purPlan = purPlanService.getPurPlanList(planNo, createTimeStartStr, createTimeEndStr, status);
		
		
		/**
		 * 导出信息,导出文件格式为.xls。
		 */
		//设置导出文件的名称
		String fileName = "采购计划表" + Time_convert.getCurrentTime_onlyDigit() + ".xls";
		//将信息存入POIHelper中
		POIHelper poiHelper = purPlanService.exportExecl(list_purPlan);
		//将poiHelper中的信息输出到response.outputStream中
		poiHelper.printToOutputStream(response, fileName);
	}
	
	
	
}
